package com.dj.mingkong.admin.biz.repository.database.music;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dj.mingkong.common.enums.StatusEnum;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.eo.BaseEo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.music.StyleEo;
import com.dj.mingkong.admin.biz.repository.database.music.mapper.StyleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 音乐风格repository
 *
 * @author zenglonghui
 * @date: 2022/5/18 10:00
 */
@Repository
public class StyleRepository extends BaseDatabaseRepository<StyleMapper, StyleEo, QueryVo> {
    @Override
    protected Wrapper<StyleEo> buildQuery(StyleEo styleEo) {
        if (styleEo == null) {
            return null;
        }
        LambdaQueryWrapper<StyleEo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(styleEo.getId() != null, StyleEo::getId, styleEo.getId());
        queryWrapper.eq(styleEo.getParentId() != null, StyleEo::getParentId, styleEo.getParentId());
        queryWrapper.likeRight(StringUtils.isNotBlank(styleEo.getName()), StyleEo::getName, styleEo.getName());
        queryWrapper.eq(styleEo.getStatus() != null, StyleEo::getStatus, styleEo.getStatus());
        queryWrapper.likeRight(StringUtils.isNotBlank(styleEo.getDescription()), StyleEo::getDescription, styleEo.getDescription());
        queryWrapper.orderByDesc(StyleEo::getCreateTime);
        return queryWrapper;
    }

    public boolean existByParentIdAndName(Long parentId, String name) {
        return lambdaQuery().eq(StyleEo::getParentId, parentId)
                .eq(StyleEo::getName, name).exists();
    }

    public List<StyleEo> listByParentId(Long parentId) {
        return lambdaQuery()
                .eq(StyleEo::getParentId, parentId)
                .orderByAsc(StyleEo::getSort).list();
    }

    public int countByIds(List<Long> ids) {
        return Math.toIntExact(lambdaQuery()
                .in(BaseEo::getId, ids)
                .eq(StyleEo::getStatus, StatusEnum.VALID.getIntVal())
                .count());

    }
}
